package hut.ai.team.object;

import hut.ai.team.screen.MainScreen;
import hut.ai.team.table.VanCo;


public class Phao  {
	public Phao() {
		super();
	}
	
	static QuanCo qTuong1;
	static QuanCo qTuong2;

	private static int[][][] score = {
            {
               {50, 50, 50, 50, 50, 50, 50, 50, 50}, 		/* RED */
               {50, 50, 50, 50, 50, 50, 50, 50, 50},
               {50, 51, 53, 53, 55, 53, 53, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 51, 51, 51, 51, 51, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50}
            },
            {
               {50, 50, 50, 50, 50, 50, 50, 50, 50}, 		/* BLACK */
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 51, 51, 51, 51, 51, 51, 50},
               {50, 51, 50, 50, 50, 50, 50, 51, 50},
               {50, 51, 53, 53, 55, 53, 53, 51, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50},
               {50, 50, 50, 50, 50, 50, 50, 50, 50}
            }
        };
	
	public static int getDiemPhao(QuanCo q){
		int d = 0;
		if(q.Phe == 2){
			if(q.TrangThai == 1)
				d = 30 + score[1][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 70 + score[1][q.Hang][q.Cot];
			else d = 0;
		}else{
			if(q.TrangThai == 1)
				d = 30 + score[0][q.Hang][q.Cot];
			else if(q.TrangThai == 2)
				d = 70 + score[0][q.Hang][q.Cot];
			else
				d = 0;
		}
		return d;
	}

	public static void getQuanTuong() {
		if (MainScreen.getInstance().mode == 2) {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1) {
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for (int i = 0; i < MainScreen.getInstance().n2.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n2.dsQuanCo.get(i).Loai == 1) {
					qTuong2 = MainScreen.getInstance().n2.dsQuanCo.get(i);
					break;
				}
			}
		} else {
			for (int i = 0; i < MainScreen.getInstance().n1.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().n1.dsQuanCo.get(i).Loai == 1) {
					qTuong1 = MainScreen.getInstance().n1.dsQuanCo.get(i);
					break;
				}
			}
			for (int i = 0; i < MainScreen.getInstance().computer.dsQuanCo.size(); i++) {
				if (MainScreen.getInstance().computer.dsQuanCo.get(i).Loai == 1) {
					qTuong2 = MainScreen.getInstance().computer.dsQuanCo.get(i);
					break;
				}
			}
		}
	}
	
	public static int KiemTra(QuanCo c, int i, int j) {
			boolean turn = true;
			getQuanTuong();
	        int count = 0;

	        if (i < 0 || i > 9 || j < 0 || j > 8) turn = false;
	        if (i != c.Hang && j != c.Cot) turn = false;
	        if (i >= 0 && i <= 9 && j >= 0 && j <= 8 && (i == c.Hang || j == c.Cot))
	        {
	            if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == true)
	            {
	                if (i == c.Hang && j >= 0 && j <= 8)
	                {
	                    if (j > c.Cot)
	                        for (int k = c.Cot + 1; k <= j; k++)
	                        {
	                            if (MainScreen.getInstance().mBanCo.ViTri[i][k].Trong == false)
	                            {
	                                turn = false;
	                                break;
	                            }
	                        }
	                    if (j < c.Cot)
	                        for (int k = j; k <= c.Cot - 1; k++)
	                        {
	                            if (MainScreen.getInstance().mBanCo.ViTri[i][k].Trong == false)
	                            {
	                                turn = false;
	                                break;
	                            }
	                        }
	                }
	                if (j == c.Cot && i >= 0 && i <= 9)
	                {
	                    if (i > c.Hang)
	                        for (int k = c.Hang + 1; k <= i; k++)
	                        {
	                            if (MainScreen.getInstance().mBanCo.ViTri[k][j].Trong == false)
	                            {
	                                turn = false;
	                                break;
	                            }
	                        }
	                    if (i < c.Hang)
	                        for (int k = i; k <= c.Hang - 1; k++)
	                        {
	                            if (MainScreen.getInstance().mBanCo.ViTri[k][j].Trong == false)
	                            {
	                                turn = false;
	                                break;
	                            }
	                        }
	                }
	            }
	            if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false)
	            {
	                if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe != c.Phe)
	                {
	                    if (i == c.Hang && j >= 0 && j <= 8)
	                    {
	                        if (j > c.Cot)
	                            for (int k = c.Cot + 1; k < j; k++)
	                            {
	                                if (MainScreen.getInstance().mBanCo.ViTri[i][k].Trong == false) count++;

	                            }
	                        if (j < c.Cot)
	                            for (int k = j + 1; k <= c.Cot - 1; k++)
	                            {
	                                if (MainScreen.getInstance().mBanCo.ViTri[i][k].Trong == false) count++;
	                            }
	                    }
	                    if (j == c.Cot && i >= 0 && i <= 9)
	                    {
	                        if (i > c.Hang)
	                            for (int k = c.Hang + 1; k < i; k++)
	                            {
	                                if (MainScreen.getInstance().mBanCo.ViTri[k][j].Trong == false) count++;

	                            }
	                        if (i < c.Hang)
	                            for (int k = i + 1; k <= c.Hang - 1; k++)
	                            {
	                                if (MainScreen.getInstance().mBanCo.ViTri[k][j].Trong == false) count++;
	                            }
	                    }
	                    if (count != 1) turn = false;
	                }
	                if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == c.Phe) turn = false;
	            }                
	        }
	     // Kiểm tra chống tướng
		if (MainScreen.getInstance().mode == 2) {
			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong2.Cot) {
				int ct = 0;
				if (j != c.Cot) {
					MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
					for (int t = qTuong1.Hang + 1; t < qTuong2.Hang; t++) {
						if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
							ct++;
					}
					if (ct == 0)
						turn = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
					if(MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
				}
			}
		} else {
			if (qTuong1.Cot == qTuong2.Cot && c.Cot == qTuong1.Cot) {
			int ct = 0;
			if (j != c.Cot) {
				MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
				for (int t = qTuong2.Hang + 1; t < qTuong1.Hang; t++) {
					if (MainScreen.getInstance().mBanCo.ViTri[t][c.Cot].Trong == false)
						ct++;
					}
					if (ct == 0)
						turn = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Phe = c.Phe;
					MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai = c.TrangThai;
					if(MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[c.Hang][c.Cot].Loai = c.Loai;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}
	
	public static int TuongAnToan(QuanCo c, int i, int j) {
		boolean turn = true;
		getQuanTuong();
		if (turn == true) {
			int ho, co, tempTrangThai, tempTTBiAn = 0;
			QuanCo temp = null;

			ho = c.Hang;
			co = c.Cot;
			tempTrangThai = c.TrangThai;
			if (MainScreen.getInstance().mode == 2) {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().n1.Phe) {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n2.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n2.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n2.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n2.dsQuanCo.get(k);
						}
					}
				}
			} else {
				if (MainScreen.getInstance().mBanCo.ViTri[i][j].Trong == false) {
					if (MainScreen.getInstance().mBanCo.ViTri[i][j].Phe == MainScreen.getInstance().computer.Phe) {
						for (int k = 0; k < MainScreen.getInstance().computer.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().computer.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().computer.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().computer.dsQuanCo.get(k);
						}
					} else {
						for (int k = 0; k < MainScreen.getInstance().n1.dsQuanCo.size(); k++) {
							if (MainScreen.getInstance().n1.dsQuanCo.get(k).Hang == i && MainScreen.getInstance().n1.dsQuanCo.get(k).Cot == j)
								temp = MainScreen.getInstance().n1.dsQuanCo.get(k);
						}
					}
				}
			}
			// Giả sử quân cờ được đi để kiểm tra Tướng mình có bị chiếu ko
			MainScreen.getInstance().mBanCo.OCoTrong(c.Hang, c.Cot);
			MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = 2;
			MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = c.Loai;
			if(temp != null){
				tempTTBiAn = temp.TrangThai;
				VanCo.AnQuanCo(temp);
			}
			
			c.Hang = i;
			c.Cot = j;
			c.TrangThai = 2;
			// Kiểm tra
			if (MainScreen.getInstance().mode == 2) {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			} else {
				if (c.Phe == MainScreen.getInstance().n1.Phe) {
					if (VanCo.ChieuTuong(qTuong1) == true)
						turn = false;
				} else {
					if (VanCo.ChieuTuong(qTuong2) == true)
						turn = false;
				}
			}
			// Trả lại những gì đã giả sử ở trên ^^!
			c.Hang = ho;
			c.Cot = co;
			c.TrangThai = tempTrangThai;
			MainScreen.getInstance().mBanCo.OCoTrong(i, j);
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Trong = false;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].Phe = c.Phe;
			MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai = c.TrangThai;
			if(MainScreen.getInstance().mBanCo.ViTri[ho][co].TrangThai == 1)
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.LoaiCL;
			else
				MainScreen.getInstance().mBanCo.ViTri[ho][co].Loai = c.Loai;
			if(temp != null){
				if (temp.Phe != 0) {
					temp.TrangThai = tempTTBiAn;
					VanCo.HoiSinhQuanCo(temp);
					MainScreen.getInstance().mBanCo.ViTri[i][j].Trong = false;
					MainScreen.getInstance().mBanCo.ViTri[i][j].Phe = temp.Phe;
					MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai = temp.TrangThai;
					if(MainScreen.getInstance().mBanCo.ViTri[i][j].TrangThai == 1)
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.LoaiCL;
					else
						MainScreen.getInstance().mBanCo.ViTri[i][j].Loai = temp.Loai;
				}
			}
		}
		if (turn)
			return 1;
		else
			return 0;
	}
}
